<?php
require_once ("secure_area.php");
require_once ("interfaces/idata_controller.php");
class Tax_configs extends Secure_area implements iData_controller 
{
	function __construct()
	{
		parent::__construct('tax_configs');
	}
	
	function get_sortable_data()
	{
		$sortable_data = array(); 
		$all_tax = $this->Tax_config->get_all();
		foreach($all_tax->result() as $row)
		{
			$raw_data = array();
			$tax_id = $row->tax_id;
			$name = $row->name;
			$exceeding = $row->exceeding;
			//Get tax Components
			$tax_comp_result = $this->Tax_config->get_tax_coponents($tax_id);
			$tax_comp_string =  '';
			foreach($tax_comp_result->result() as $row)
			{
				$tax_comp_string .= $row->amount.' @ '.$row->percent_value.'%'.'&nbsp;&nbsp;&nbsp;';
			}
			$tax_comp_string .= 'Exceeding @ '.$exceeding.'%';
			
			if(empty($tax_comp_string))
			{
				$tax_comp_string = 'None';
			}
			$raw_data[] = $tax_id;
			$raw_data[] = $name;
			$raw_data[] = $tax_comp_string;
			
			$sortable_data[] = $raw_data;	
		}	
		return $sortable_data;
	}
	
	
	function index()
	{
		$sortable_data = array(); 
		$sortable_data = $this->get_sortable_data();
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width() + 200;		
		$data['manage_table']=get_tax_manage_table($sortable_data,$this);
		$this->load->view('tax/manage',$data);
	}
	
	function search()
	{
		$sortable_data = array(); 
		$search=$this->input->post('search');
		$sortable_data = $this->get_sortable_data();
		$data_rows=get_tax_manage_table_data_rows($sortable_data,$this);
		echo $data_rows;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Tax_config->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	function get_row()
	{
		$sortable_data = array(); 
		$item_id = $this->input->post('row_id');
		$all_tax = $this->Tax_config->get_info($item_id);
		foreach($all_tax->result() as $row)
		{
			$raw_data = array();
			$tax_id = $row->tax_id;
			$name = $row->name;
			//Get tax Components
			$tax_comp_result = $this->Tax_config->get_tax_coponents($tax_id);
			$tax_comp_string =  '';
			foreach($tax_comp_result->result() as $row)
			{
				$tax_comp_string .= $row->amount.' @ '.$row->percent_value.'%'.'&nbsp;&nbsp;&nbsp;';
			}
			if(empty($tax_comp_string))
			{
				$tax_comp_string = 'None';
			}
			
			$raw_data[] = $tax_id;
			$raw_data[] = $name;
			$raw_data[] = $tax_comp_string;
			
			$sortable_data[] = $raw_data;	
		}	
		$data_row=get_tax_data_row($sortable_data,$this);
		echo $data_row;
	}
	
	function view($item_id=-1)
	{
		$data['tax_info']=$this->Tax_config->get_info($item_id);
		$data['tax_componets']=$this->Tax_config->get_tax_coponents($item_id);
		//$data['default_tax_2_rate']=($item_id==-1) ? $this->Appconfig->get('default_tax_2_rate') : '';
		$this->load->view("tax/form",$data);
	}
	
	function edit_tax($tax_id=-1)
	{				
		$tax_info = $this->Tax_config->get_tax_for_editing($tax_id);
		$tax = $tax_info['query_taxs']->row();
		$tax_items = edit_tax_items_manage_table($tax_info['query_tax_items'],$this);
		$data['manage']=$tax_items;
		$data['tax']=$tax;
		$data['controller_name']=strtolower($this->uri->segment(1));
				
		$this->load->view("tax/edit_tax", $data);
	}
	
	function save($item_id=-1)
	{
		$item_data = array(
		'name'=>$this->input->post('name'),
		'exceeding'=>$this->input->post('exceeding')
		);
		///////////////////////////////////////////////////////////////////////////////////////////
		
		//Order items
		$order = array();
		$amount = array();
		$percent_value = array();
		$final_pair_array = array();
			
		foreach($this->input->post('order') as $datax)
		{
			$order[] = $datax;
		}
		
		foreach($this->input->post('amount') as $datax)
		{
			$amount[] = $datax;
		}
		
		foreach($this->input->post('percent_value') as $datax)
		{
			$percent_value[] = $datax;
		}
		
		//Merge the amount and percent_value
		for($x=0; $x < count($amount); $x++)
		{
			$pair_array = array();
			$pair_array[0] = $amount[$x];
			$pair_array[1] = $percent_value[$x];
			$final_pair_array[] = $pair_array;	
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($order); $x++)
		{
			$final_array[$order[$x]] = $final_pair_array[$x];	
		}
			
		//save the order
		$return_id = $this->Tax_config->save($item_data,$item_id);
		if($return_id)
		{	
			$tax_id = $item_data['tax_id'];
			$number_of_processed = 0;
			foreach($final_array as $key => $value)
			{
			  $amount = $value[0];//cost
			  $percent_value = $value[1];//quantity
			   	 	 	
			  $compo_data = array(
			  'tax_id'=>$tax_id,
			  'order'=>$key,
			  'amount'=>$amount,
			  'percent_value'=>$percent_value
			  );
			  
			  $db_result = $this->Tax_config->save_tax_items($compo_data);
			  if($db_result)
			  {
				  $number_of_processed++;
			  }
			}
				
			if($number_of_processed == count($final_array))
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding').' '.
				$item_data['name'],'item_id'=>$tax_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			$item_data['name'],'item_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	function update_tax()
	{			
		$item_data = array(
		'name'=>$this->input->post('name'),
		'exceeding'=>$this->input->post('exceeding')
		);
		
		$tax_id = $this->input->post('hidden_tax');
		$hidden_empty = $this->input->post('hidden_empty');
		
		if($hidden_empty == 1)
		{
			//Order items
			$order = array();
			$amount = array();
			$percent_value = array();
			$item_update_id = array();
			$final_pair_array = array();

			foreach($this->input->post('order') as $datax)
			{
				$order[] = $datax;
			}
		
			foreach($this->input->post('amount') as $datax)
			{
				$amount[] = $datax;
			}
		
			foreach($this->input->post('percent_value') as $datax)
			{
				$percent_value[] = $datax;
			}
		
			foreach($this->input->post('item_update_id') as $datax)
			{
				$item_update_id[] = $datax;
			}
		
			//Merge the amount and percent_value
			for($x=0; $x < count($amount); $x++)
			{
				$pair_array = array();
				$pair_array[0] = $amount[$x];
				$pair_array[1] = $percent_value[$x];
				$pair_array[2] = $item_update_id[$x];
				$final_pair_array[] = $pair_array;	
			}
			
			//Put them in One array as key and value
			for($x=0; $x < count($order); $x++)
			{
				$final_array[$order[$x]] = $final_pair_array[$x];	
			}
		}
		
		//save the order
		if($hidden_empty == 0)
		{
			if($this->Tax_config->update_tax($item_data,$tax_id))
			{		
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('tax_successful_adding').' '.
				$item_data['name'],'item_id'=>$tax_id));
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('tax_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		else
		{
			if($this->Tax_config->update_tax($item_data,$tax_id))
			{		
				$number_of_processed = 0;
				foreach($final_array as $key => $value)
				{
					$amount = $value[0];//cost
					$percent_value = $value[1];//quantity
					$update_id = $value[2];//update id
			   	 	 	
					$compo_data = array(
					'tax_id'=>$tax_id,
					'order'=>$key,
					'amount'=>$amount,
					'percent_value'=>$percent_value
					);
				
					$db_result = $this->Tax_config->update_tax_items($compo_data,$update_id);
					if($db_result)
					{
						$number_of_processed++;
					}
				}
				
				if($number_of_processed == count($final_array))
				{
					echo json_encode(array('success'=>true,'message'=>$this->lang->line('tax_successful_adding').' '.
					$item_data['name'],'item_id'=>$tax_id));
				}
				else
				{
					echo json_encode(array('success'=>false,'message'=>$this->lang->line('tax_error_adding_updating').' '.
					$item_data['name'],'item_id'=>-1));
				}
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('tax_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		
		
	}
	
	
	function delete()
	{
		$items_to_delete=$this->input->post('ids');
		
		if($this->Tax_config->delete_list($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('tax_successful_deleted').' '.
			count($items_to_delete).' '.$this->lang->line('items_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	function delete_tax_item()
	{
		$items_to_delete = $this->input->post('row_id');
		$tax_id = $this->input->post('tax_id');
		
		if($this->Tax_config->delete_tax_item($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('tax_successful_deleted').' '.
			'','item_id'=>$tax_id));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 350;
	}
	
////////////  MEK /////////////////////////////////////////////////////////////////////////////////
	

	
	
	
	
}
?>